// pages/result/result.js
const app = getApp();
const util = require('../../../../utils/util.js');
const tools = require('../../../../utils/tools.js');
const api = require('../../../../utils/api.js');
const innerAudioContext = app.globalData.innerAudioContext; //引入音频播放器

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		VID: null,//练习语句id
		recordFilePath: null, //录音文件路径
		result: null,//录音结果
		record_scale: 100, //分制
		difficultWords: null,//难点词汇数组
		noWords: false, //是否没有难点词汇
		isPlaying: false, //是否正在播放
		voiceDuration: null,//用户录音时长
		playTimeText: null,//用户录音时长事件格式
		remain: 0,//播放录音剩余时间
		playtimeKeeping: 0,//播放录音计时
	},

	onLoad(options) {
		console.log('传入的数据', options)
		this.setData({
			recordFilePath: options.filePath,
			VID: options.VID,
			voiceDuration: options.voiceDuration,
			playTimeText: tools.timeFormat(options.voiceDuration),
			result: app.globalData.result,
		})
		this.difficultWords();
	},

	//获取难点词汇
	difficultWords() {
		let _self = this;
		let dataDic = {
			VID: _self.data.VID,
			//VID: 35697
		};
		api.methodGetDifficultWords({
			dataDic,
			success: (res) => {
				console.log('难点词汇数据', res)
				if(res.data.data.DifficultWords.length<=0){
					_self.setData({
						noWords: true
					})
				}
				// 将|替换为 \n 换行符
				for(let i  = 0; i < res.data.data.DifficultWords.length; i++){
					res.data.data.DifficultWords[i].WordClass = res.data.data.DifficultWords[i].WordClass.replace(/\|/g, "\n");
					res.data.data.DifficultWords[i].WordDerivative = res.data.data.DifficultWords[i].WordDerivative.replace(/\|/g, "\n");
				}
				_self.setData({
					difficultWords: res.data.data.DifficultWords,
					noWords: false
				})
			},
			fail: (res) => {
				console.log(res);
			},
			complete: (res) => {
				console.log(res.data);
			},
		})
	},
	toKouyu() {
		wx.navigateTo({
			url: '/pages/spoken/spoken',
		})
	},
	//播放用户录音
	playRecord() {
		console.log("播放用户录音")
		let _self = this;
		if (_self.data.isPlaying) {
			console.log("正在播放录音，请稍后再点")
			return
		}
		app.aldstat.sendEvent('结果-点击我的录音');
		clearInterval(timer_play);
		let timer_play = setInterval(function () {
			_self.setData({
				playtimeKeeping: _self.data.playtimeKeeping + 1,
				playTimeText: tools.timeFormat(_self.data.playtimeKeeping + 1)
			});
			console.log('计时', _self.data.playtimeKeeping);
			//当剩余时间为零时，停止播放录音
			if (_self.data.playtimeKeeping == _self.data.voiceDuration) {
				_self.setData({
					isPlaying: false,
				})
				clearInterval(timer_play);
			}
		}, 1000);
		_self.timer_play = timer_play
		_self.setData({
			isPlaying: true,
			playtimeKeeping: 0,
			playTimeText: "00:00"
		})
		innerAudioContext.src = _self.data.recordFilePath;
		innerAudioContext.play();

		innerAudioContext.onEnded((res) => {
			console.log('正常停止播放', res)
		})
	},
	//停止播放用户录音
	stopRecord() {
		console.log("停止用户录音")
		let _self = this;
		clearInterval(_self.timer_play);
		innerAudioContext.stop();
		_self.setData({
			isPlaying: false,
			playTimeText: tools.timeFormat(_self.data.voiceDuration)
		})
	},
	// 点击口语训练营
	aldSpoken() {
		app.aldstat.sendEvent('结果-点击口语训练营');
	},
	// 点击求助外教
	aldTeacher() {
		app.aldstat.sendEvent('结果-点击求助外教');
	}
})